<template>
	<!-- 导航栏 -->
	
	<view class="content_box catgory-page">
		<view class="catgory-head">
			<!-- <view class="catgory-head-title">
				分类
			</view> -->
			<view class="catgory-head-search">
				<view class="catgory-head-search-logo">
					<span>Magisches</span>
					<span>Element</span>
				</view>
				<!-- @change="onSearch" -->
				<!-- <view class="" slot="content">
					<u-search placeholder="搜索"  @search="onSearch" @custom="onSearch" @clear="clearSearch" v-model="searchVal" :showAction="true" 
					action-style=''
					bg-color="#2c50d4" border-color="#878787"
					width="500" height="60" ></u-search>
				</view> -->
				<view class="catgory-head-search-div">
					<view class="catgory-head-search-text1">
						<image src="/static/images/search.png">
						<input placeholder="搜索" type="text" placeholder-style="color: #444;"
						v-model="searchVal" @keydown.enter="onSearch"></input>
					</view>
					<button class="catgory-head-search-text2" @click="onSearch">搜索</button>
				</view>
			</view>
		</view>
		<view class="u-flex u-col-center wrapper-box">
			<view class="scroll-box classify-div">
				<scroll-view class="left u-flex-col u-col-center" enable-flex enable-back-to-top scroll-y>
					<view class="type-list u-ellipsis-1" :class="[{ 'list-active': listId == index }]" v-for="(item, index) in categoryData" :key="index" @tap="onType(index)">
						<view class="line" :class="[{ 'line-active': listId == index }]"></view>
						{{ item.name }}
					</view>
					<view class="hack-tabbar"></view>
				</scroll-view>
			</view>
			<view style="height: 100%;width: 100%;">
				<scroll-view @scrolltolower="loadMore" scroll-y class="scroll-box" enable-back-to-top scroll-with-animation>
					<view class="right" >
						<image class="type-img" v-if="categoryData[listId].image" :src="categoryData[listId].image" mode="aspectFill"></image>
						<view class="item-list">
							<view class="item-box u-flex" style="flex-direction: column;flex-wrap: nowrap;">
								<view
									class="u-flex-col u-col-center goods-item"
                  @tap="jump('/pages/goods/detail', { id: goods.id })"
									v-for="(goods, index1) in goodsList"
									:key="goods.id"
								>
									<view class="item-img-div">
										<image class="item-img" lazy-load :src="goods.image" mode="aspectFill"></image>
									</view>
									<view class="item-title-div">
										<text class="item-title u-ellipsis-1">
											{{ goods.title }}
										</text>
										<text class="item-freight">
											库存：{{ goods.stock }}
										</text>
										<view class="item-price-div">
											<view>
												<view class="item-price">{{ goods.price }}</view>
												<view class="item-price2">{{ goods.original_price }}</view>
											</view>
                      <view class="buy_now">立即选购</view>
										</view>
									</view>
									
								</view>
							</view>
						</view>

						<!-- 缺省页 -->
						<shopro-empty
							v-if="isEmpty"
							marginTop="200rpx"
							:image="$IMG_URL + '/imgs/empty/empty_goods.png'"
							tipText="暂无该商品，还有更多好货等着你噢~"
						></shopro-empty>

						<!-- 加载更多 -->
						<u-loadmore v-if="!isEmpty" height="80rpx" :status="loadStatus" icon-type="flower" color="#ccc" />
						<view class="hack-tabbar"></view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
export default {
	components: {},
	data() {
		return {
			listId: 0,
			categoryData: {},
			categoryID: 0, //分类id
			isEmpty: false,
			currentPage: 1,
			lastPage: 1,
			keywords:'',
			searchVal: '',
			loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
			goodsList: [] ,//商品数据
		};
	},
	props: {
		categoryStyleId: {
			//分类样式ID
			type: Number,
			default: 0
		}
	},
	computed: {},
	created() {
		console.log('%c当前分类:一级分类', 'color:green;background:yellow');
		// this.getGoodsList();
		this.getCategory();
	},

	methods: {
		...mapActions(['getCartList', 'changeCartList']),
		// 获取分类
		getCategory() {
			this.$http('category.list', {
				id: this.categoryStyleId
			}).then(res => {
				if (res.code === 1) {
										console.log(res)
					this.categoryData = res.data.children;
					this.categoryID = res.data.children[0].id;
					this.getGoodsList();
				}
			});
		},
		
		// 获取分类商品
		getGoodsList() {
			let that = this;
			that.$http('goods.lists', {
				category_id: that.categoryID,
				page: that.currentPage,
				keywords: that.keywords
			}).then(res => {
				if (res.code === 1) {
					that.goodsList = [...that.goodsList, ...res.data.data];
					that.isEmpty = !that.goodsList.length;
					that.lastPage = res.data.last_page;
					that.loadStatus = that.currentPage < res.data.last_page ? 'loadmore' : 'nomore';
				}
			});
		},
		// 键盘搜索,输入搜索
		onSearch() {
			this.goodsList = [];
			this.currentPage = 1;
			this.keywords = this.searchVal;
			this.clear();
			this.$u.debounce(this.getGoodsList);
		},
		clear() {
			this.leftList = [];
			this.rightList = [];
			this.leftHeight = 0;
			this.rightHeight = 0;
			this.tempList = [];
		},
		// 商品底部
		loadMore() {
			if (this.currentPage < this.lastPage) {
				this.currentPage += 1;
				this.getGoodsList();
			}
		},

		onType(index) {
			this.listId = index;
			this.categoryID = this.categoryData[index].id;
			this.goodsList = [];
			this.currentPage = 1;
			this.getGoodsList();
		},
		// 路由跳转
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		},
		onMin(g,key) {
			let that = this;
			this.$http('cart.edit2', {
				id:g.id,
				sku: g.sku_price[0].id,
				num: 0
			}).then(res => {
				that.goodsList[key].cart_count=0;
				// uni.redirectTo({
				//     url: 'pages/index/category' 
				// });
			});
			 
		},
		// 更改商品数
		changeNum(e, g,key) {
			let that = this;
			this.$http('cart.edit2', {
				id:g.id,
				sku: g.sku_price[0].id,
				num: e.value
			}).then(res => {
				that.goodsList[key].cart_count=e.value;
			});
		},
		// 到达最小值
		onMin2(g) {
			uni.showModal({
				title: '删除提示',
				confirmColor: '#f0c785',
				content: `是否确认从购物车中删除此商品?`,
				success: res => {
					res.confirm && this.changeCartList({ ids: [g.id], art: 'delete' });
				}
			});
		},
		// // 更改商品数
		// async changeNum(e, g) {
		// 	uni.showLoading({
		// 		mask: true
		// 	});
		// 	e.value > 0 && this.changeCartList({ ids: [g.id], goodsNum: e.value, art: 'change' });
		// 	uni.hideLoading();
		// 	await this.getCartList();
		// },
	}
};
</script>

<style lang="scss">
.hack-tabbar {
	height: calc(100rpx + env(safe-area-inset-bottom) / 2);
	width: 100%;
}
.content_box {
	margin-top: 1upx;
	display: flex;
	flex-direction: column;
	overflow: hidden;
	height: 100%;
}

.wrapper-box {
	flex: 1;
	margin-top: 1upx;
	height: 100%;
}
.classify-div{
	// border-right: 1rpx solid #353535;
	box-shadow: 4rpx 0rpx 6rpx 2rpx rgba(1,1,1,0.16);
	background-color:#fff;
}
.item-img-div{
	min-width: 158rpx;
	min-height: 158rpx;
}
.item-title-div{
	display: flex;
	flex-direction: column;
	margin-left: 14rpx;
	.item-freight{
		width: 192rpx;
		color: #444;
		font-size: 21rpx;
		opacity: 63%;
		margin-bottom: 24rpx;
	}
}
.item-price-div{
	min-width: 270rpx;
    display: flex;
    justify-content: space-between;
}
.scroll-box {
	height: 100%;
	flex: 1;
}

.left {
	width: 258rpx;
	height: 100%;
	flex: 1;
	box-shadow: 4rpx 0rpx 6rpx 2rpx rgba(1,1,1,0.16);
	.list-active {
		background-color:#bebebe!important;;
		color: #444 !important;
		font-weight: bold !important;
		opacity: 1!important;
	}

	.line-active {

	}

	.type-list {
		height: 83rpx;
		position: relative;
		width: 258rpx;
		padding-left: 16rpx;
		line-height: 84rpx;
		font-size: 28rpx;
		font-weight: normal;
		color: #444;
		opacity: 63%;
		.line {
			width: 10upx;
			height: 100%;
			position: absolute;
			left: 0;
		}
	}
}

.right {
	// padding: 0 30upx;
	flex: 1;
	height: 100%;

	.type-img {
		width: 505rpx;
		height: 150rpx;
		background-color:#bebebe;
		margin: 20rpx 0;
		border-radius: 10rpx;
	}

	.item-list {
		.type-box {
			height: 84rpx;
			.type-title {
				font-size: 28rpx;
				font-weight: bold;
			}
			.more {
				font-size: 26rpx;
				color: #444;
			}
		}

		.item-box {
			flex-wrap: wrap;
			// width: 504rpx;
			.goods-item {
				padding:0 25rpx 22rpx 27rpx;
				margin-bottom: 22rpx;
				width: 100%;
				flex-direction: row;
				align-items: end;
				border-bottom: 1rpx solid rgba(255, 255, 255, 0.17);
				&:nth-child(2n) {
					margin-right: 0;
				}

				.item-img {
					width: 158rpx;
					height: 158rpx;
					border-radius: 8rpx;
					background-color:#bebebe;
				}

				.item-title {
					font-size: 24rpx;
					line-height: 30rpx;
					color: #444;
					margin-top: 10rpx;
					margin-bottom: 12rpx;
					width: 256rpx;
					text-align: left;
				}
				.item-price {
					font-size: 21rpx;
					font-weight: 400;
					text-align: left;
					margin: auto 0 10rpx 0;
					&::before {
						content: '￥';
						font-size: 21rpx;
					}
				}
				.item-price2 {
					font-size: 13rpx;
					font-weight: 400;
					text-align: left;
					margin: auto 14rpx 10rpx 8rpx;
					text-decoration: line-through;
				}
        .buy_now{
          background: #bebebe;
          color: #fff;
          font-size: 20rpx;
          text-align: center;
          width: 120rpx;
          border-radius: 30rpx;
          height: 40rpx;
          box-sizing: border-box;
          line-height: 44rpx;
          margin-top: 20rpx;
        }
			}
		}
	}
}
.catgory-page{
	color: #444;
	background-color:#fff;
}
.catgory-head{
	min-height: 120rpx;
	// margin-top: 50rpx;
}
.catgory-head-title{
	font-size: 45rpx;
	text-align: center;
	margin: 0 auto;
}
.catgory-head-search{
	display: flex;
}
.catgory-head-search-logo{
	display: flex;
	align-items: flex-start;
	flex-direction: column;
	font-size: 20rpx;
	margin: auto 50rpx auto 24rpx;
}
.catgory-head-search-div{
    width: 537rpx;
    height: 66rpx;
	display: flex;
	justify-content: space-between;
	background-color:#ececec;
	border-radius: 33rpx;
	margin: 5rpx;
	border: 2rpx solid rgba(255,255,255,0.5);
}
.catgory-head-search-div image{
	width: 20rpx;
	height: 20rpx;
}
.catgory-head-search-text1{
	width: 75%;
	display: contents;
}
.catgory-head-search-text1 image{
	width: 33rpx;
	height: 33rpx;
	margin: auto 0 auto 25rpx;
}
.catgory-head-search-text1 input{
	font-size: 29rpx;
	margin: auto 0 auto 0;
	font-weight: normal;
	color: #444;
    opacity: 63%;
}
.catgory-head-search-text2{
	color: #444;
	background-color:#bebebe;
	border-radius: 50rpx;
	font-size: 24rpx;
	width: 125rpx;
	height: 67rpx;
  line-height: 67rpx;
	margin: 0;
	opacity: 63%;
}


//选择数量
.u-icon-plus, .u-icon-minus {
    justify-content: initial!important;
}
.u-icon--right {
    width: 60rpx;
}
</style>
